#import "YQPShopNameModel.h"

#import "MYShopVC.h"
#import "YQNavTitleView.h"
#import "YQSearchBaseBar.h"
#import "MYShopSearchVC.h"
#import "MYShopListVC.h"
#import <JXCategoryView/JXCategoryView.h>
@interface MYShopVC ()<YQSearchBaseBarDelegate,JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) YQSearchBaseBar * searchBar;
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@property (nonatomic, copy) NSArray * titles;

@property (nonatomic, strong) NSMutableArray * goodTitles;

@property (nonatomic, strong) NSMutableArray * goodNames;

@end

@implementation MYShopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.goodTitles = NSMutableArray.new;
    
    self.goodNames = NSMutableArray.new;

    //获取一级商品列表分类
    [self getFCatalogListApi];
}

#pragma mark - 查询一级商品列表
- (void)getFCatalogListApi
{
    [self.goodTitles removeAllObjects];
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_SKU_ALL_CATALOGF_URL parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            NSArray *goodTitles = [YQPShopNameModel mj_objectArrayWithKeyValuesArray:responseObject.data];
            
            [self.goodTitles addObjectsFromArray:goodTitles];
            for (YQPShopNameModel *model in goodTitles) {
                if (model.catalog1Name.length && !IsStrEmpty(model.catalog1Name)) {
                    [self.goodNames addObject:model.catalog1Name];

                }
            }
            [self setUpSubiews];

        }else{
            kMBMesHUD(responseObject.msg)
        }
        YQHIDELOADINGTIPS
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
    }];
}

-(void)setupView {
    [super setupView];
}

#pragma mark - 创建分类控件
- (void)setUpSubiews
{
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = UIColor.whiteColor;
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    [bgView addSubview:self.searchBar];
    
    
    self.titles =@[kYQLS(@"家居与睡眠"),
                   kYQLS(@"玩具与教育"),
                   kYQLS(@"哺育产品"),
                   kYQLS(@"幼儿健康"),
                   kYQLS(@"旅行/出门")];
    self.categoryView = [[JXCategoryTitleView alloc] init];
    self.categoryView.delegate = self;
    self.categoryView.titles = self.goodTitles.count?self.goodNames:self.titles;
    self.categoryView.defaultSelectedIndex = 0;
    //    self.categoryView.cellSpacing = 82;
    self.categoryView.titleSelectedColor = HexColor(0x79C4C7);
    self.categoryView.titleColor = HexColor(0xA0A0A0);
    self.categoryView.titleFont = kSystemMedium(12);
    self.categoryView.titleSelectedFont = kSystemMedium(12);
    self.categoryView.contentEdgeInsetLeft = 12;
    self.categoryView.backgroundColor = UIColor.whiteColor;
    self.categoryView.titleColorGradientEnabled = YES;
    //    self.categoryView.titleLabelZoomEnabled = YES;
    self.categoryView.averageCellSpacingEnabled = YES;
    
    [self.view addSubview:self.categoryView];
    
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    self.listContainerView.defaultSelectedIndex = 0;
    [self.view addSubview:self.listContainerView];
    
    self.categoryView.contentScrollView = self.listContainerView.scrollView;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    self.listContainerView.frame = CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height - 44);
}


#pragma mark - JXCategoryViewDelegate --- 选项卡点击

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
    
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}
#pragma mark - JXCategoryViewDelegate end
#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    YQPShopNameModel *model = self.goodTitles[index];
    MYShopListVC *listVC = [[MYShopListVC alloc] init];
    listVC.catalog1Id = model.catalog1Id;
    [self addChildViewController:listVC];
    return listVC;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.goodTitles.count?self.goodTitles.count:self.titles.count;
}

#pragma mark - JXCategoryListContainerViewDelegate end


#pragma makr -- lazy
-(YQSearchBaseBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[YQSearchBaseBar alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH - 24, 28)];
        _searchBar.top = 12;
        _searchBar.delegate = self;
        _searchBar.placeholderStr = kYQLS(@"搜索您要的物品");
    }
    return _searchBar;
}

#pragma makr -- lazy end
#pragma makr -- <YQSearchBaseBarDelegate>
- (BOOL)yq_searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    MYShopSearchVC *searchVC = [[MYShopSearchVC alloc] init];
    kYQPUSH(searchVC);
    return NO;
}
#pragma makr -- <YQSearchBaseBarDelegate> end

@end
